import Promise from 'promise';
import * as ResultMsg from '../constrants/ResultMsg';
import { ProfileModel } from '../models';

const fileds = null;

export function findOne(appId) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  const query = ProfileModel.findOne({ appId });
  return query.exec();
}

export function findOneById(appId, id) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  const query = ProfileModel.findOne({ appId, _id: id });
  return query.exec();
}

export function update(appId, id, data) {
  if (typeof id !== 'string' && typeof id !== 'object') throw new Error(ResultMsg.NO_ID);
  if (typeof data !== 'object') throw new Error(ResultMsg.NOT_OBJECT);
  const query = ProfileModel.findByIdAndUpdate(id, { $set: data });
  return query.exec();
}

export function create(appId, { guide, isHtml }) {
  if (typeof appId !== 'string') throw new Error(ResultMsg.NO_APP_ID);
  if (typeof guide !== 'string') throw new Error(ResultMsg.NOT_STRING);
  if (typeof isHtml === 'boolean') throw new Error(ResultMsg.NOT_BOOLEAN);

  const doc = new ProfileModel();
  doc.appId = appId;
  doc.guide = guide;
  doc.isHtml = isHtml;
  return doc.save();
}
